package ry.wiki.reyvcodeopen;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

@Component
public class OCAutoRunner implements ApplicationRunner {

    @Autowired
    private OCService ocService;

    String version = "1.0";

    String prefixPath = "";

    @Override
    public void run(ApplicationArguments args) throws Exception {
        while (true) {
            System.out.println("请输入根路径:");
            Scanner scanner = new Scanner(System.in);
            String rootPath = scanner.nextLine();
            System.out.println("请输入需要去除的前置路径:");
            prefixPath = scanner.nextLine();
            System.out.println("请输入需要切换的前置路径:");
            System.out.println("请输入BaseName:");
            String baseName = scanner.nextLine();
            System.out.println("请输入Version:");
            version = scanner.nextLine();
            System.out.println("是否确认根路径:" + rootPath + "(" + version + ")" + "?(y)");
            String isConfirm = scanner.nextLine();
            if (!"y".equals(isConfirm)) {
                System.out.println("请重新输入根路径");
                continue;
            }
            LambdaQueryWrapper<OCFileEntity> queryWrapper = new LambdaQueryWrapper<OCFileEntity>()
                    .eq(OCFileEntity::getFileName, baseName)
                    .eq(OCFileEntity::getVersion, version);
            if (ocService.exists(queryWrapper)) {
                System.out.println(baseName + "(" + version + ")" + "已经存在");
                continue;
            }
            OCFileEntity baseEntity = new OCFileEntity(version, baseName, null, -1, 1);
            ocService.save(baseEntity);
            System.out.println(baseEntity.getId());
            System.out.println("\n开始扫描文件\n");
            insertVersion(rootPath, baseEntity.getId());
            System.out.println("\n完成\n");
            System.out.println("\n\n\n\n\n\n\n");
        }

    }

    public boolean insertVersion(String path, int baseId) {
        File[] fileList = new File(path).listFiles();
        assert fileList != null;
        System.out.print("正在扫描文件夹：" + path + "\r");
        List<OCFileEntity> ocFileEntities = new ArrayList<>();
        for (File file : fileList) {
            if (file.isDirectory()) {
                if (file.getName().equals(".git")) {
                    System.out.print("\n.git文件夹已跳过扫描\n");
                    continue;
                }
                if (file.getName().equals("node_modules")) {
                    System.out.print("\nnode modules文件夹已跳过扫描\n");
                    continue;
                }
                OCFileEntity entity = new OCFileEntity(version, file.getName(), file.getPath().replaceFirst(prefixPath, ""), baseId, 1);
                ocService.save(entity);
                insertVersion(file.getPath(), entity.getId());
            } else {
                // 如果是文件则将其加入到文件数组中
                ocFileEntities.add(new OCFileEntity(version, file.getName(), file.getPath().replaceFirst(prefixPath,""), baseId, 0));
                System.out.print("扫描到文件=>" + file.getPath() + "\r");
            }
        }
        ocService.saveBatch(ocFileEntities);
        return true;
    }
}
